import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { join } from 'path';
import { NestExpressApplication } from '@nestjs/platform-express';
import { Response } from 'express';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  //const app = await NestFactory.create(AppModule);
  // Serve static files from the frontend build directory
  app.useStaticAssets(join(__dirname, '..', 'frontend', 'build'));

  // CORS 配置
  app.enableCors({
    origin: 'http://localhost:3000', // 允许的前端应用地址
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
    allowedHeaders: 'Content-Type, Accept',
  });

  // Serve the frontend application
  app.getHttpAdapter().get('/', (req, res) => {
    // Type assertion here
    (res as Response).sendFile(join(__dirname, '..', 'frontend', 'build', 'index.html'));
  });
  await app.listen(8080);
}
bootstrap();
